package com.wenwen.social.controller;

import com.wenwen.social.application.FileApplication;
import com.wenwen.social.application.dto.UserDTO;
import com.wenwen.social.controller.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "文件上传接口",tags = {"上传下载的controllers"})
@RequestMapping("/file")
public class FileController extends BaseController{

	@Autowired
	FileApplication fileApplication;

	/**
	 * 上传文件的接口:
	 * @param file
	 * @param ft
	 * @return
	 */
	@ApiOperation(value = "文件上传接口",notes = "文件上传的接口notes")
	@ApiImplicitParam(name = "fileType",value = "文件类型,1.文本,2.图片,3.视频",required = true)
	@PostMapping(value = "/upload" ,headers = "content-type=multipart/form-data")
	public ResultVO<String> upload(@ApiParam(value = "上传的文件",required = true) MultipartFile file,
	                               @RequestParam("fileType") int ft) {
		if (file.isEmpty()) {
			return new ResultVO<String>().fail("上传失败，请选择文件");
		}
		UserDTO currLoginUser = getCurrLoginUser();
		String upload = fileApplication.upload(file, ft,currLoginUser.getId());
		return new ResultVO<String>().success(upload);
	}

	/**
	 * 下载文件的接口
	 * @param response
	 * @param ft
	 * @param path
	 * @return
	 */
	@GetMapping(value = "/download")
	public ResultVO<String> download(HttpServletResponse response,
	                                 @RequestParam("fileType") int ft,
	                                 @RequestParam("exportPath") String path) {

		UserDTO currLoginUser = getCurrLoginUser();
		fileApplication.downLoad(response, ft,currLoginUser.getId(),path);
		return new ResultVO<String>().success("");
	}
}
